/*
 * Copyright (c) 2023 IUV Inc. All rights reserved.
 * 项目名称：IUV TXY
 * 文件名：DemoBaikeOnlineHourServiceImpl.java
 * Date：23-10-10 下午2:09
 * Author：Will
 */

package com.iuv.baike.service.impl;

import com.aliyun.dytnsapi20200217.models.DescribePhoneNumberOnlineTimeResponse;
import com.aliyun.dytnsapi20200217.models.DescribePhoneNumberOnlineTimeResponseBody;
import com.iuv.baike.domain.DemoBaikeOnlineHourTbl;
import com.iuv.baike.dto.OnlineHourInfo;
import com.iuv.baike.repository.DemoBaikeOnlineHourTblRepository;
import com.iuv.baike.service.DemoBaikeOnlineHourService;
import com.iuv.baike.util.HaoMaBaiKeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * DemoBaikeOnlineHourServiceImpl
 * 号码百科在网时长例子-业务实现类
 *
 * @author Will
 * @date 13:50
 */

/**
 * TODO 问题三 申明为DemoBaikeOnlineHourService接口实现的Service类
 */
@Service("demoBaikeOnlineHourService")
/*
 * END
 */
public class DemoBaikeOnlineHourServiceImpl implements DemoBaikeOnlineHourService {

    /**
     * TODO 问题四 引入DemoBaikeOnlineHourTblRepository进行数据库操作
     */
    @Autowired
    private DemoBaikeOnlineHourTblRepository demoBaikeOnlineHourTblRepository;
    /*
     * END
     */


    @Override
    public OnlineHourInfo queryOverYun(String mobile, String carrier) {


        DescribePhoneNumberOnlineTimeResponse describePhoneNumberOnlineTimeResponse =HaoMaBaiKeUtil.queryOnlineTime(mobile,carrier);
        if(describePhoneNumberOnlineTimeResponse!=null)
        {
            DescribePhoneNumberOnlineTimeResponseBody body=describePhoneNumberOnlineTimeResponse.getBody();
            if("OK".equalsIgnoreCase(body.getCode()))
            {
                DescribePhoneNumberOnlineTimeResponseBody.DescribePhoneNumberOnlineTimeResponseBodyData data=body.getData();
                if(data!=null) {
                    String onlineHour =realOnlineHour(data.getVerifyResult());
                    String carrierCode =realCarrierCode(data.getCarrierCode());
                    DemoBaikeOnlineHourTbl demoBaikeOnlineHourTbl=new DemoBaikeOnlineHourTbl();
                    demoBaikeOnlineHourTbl.setCarrierCode(carrierCode);
                    demoBaikeOnlineHourTbl.setMobile(mobile);
                    demoBaikeOnlineHourTbl.setOnlineHour(onlineHour);
                    Date now = new Date();
                    demoBaikeOnlineHourTbl.setCreateTime(now);
                    demoBaikeOnlineHourTbl.setLastUpdateTime(now);
                    demoBaikeOnlineHourTblRepository.save(demoBaikeOnlineHourTbl);
                    return changeTo(demoBaikeOnlineHourTbl);
                }
            }
        }
        return null;
    }

    @Override
    public OnlineHourInfo queryByMobile(String moblie) {
        if(moblie!=null)
        {
            DemoBaikeOnlineHourTbl demoBaikeOnlineHourTbl= demoBaikeOnlineHourTblRepository.queryTopByMobile(moblie);
            if(demoBaikeOnlineHourTbl!=null)
            {
                return changeTo(demoBaikeOnlineHourTbl);
            }
        }
        return null;
    }

    private OnlineHourInfo changeTo(DemoBaikeOnlineHourTbl demoBaikeOnlineHourTbl)
    {
        return new OnlineHourInfo(demoBaikeOnlineHourTbl.getMobile(),demoBaikeOnlineHourTbl.getOnlineHour(),demoBaikeOnlineHourTbl.getCarrierCode());
    }

    private String realOnlineHour(String onlineHour)
    {
        String realOnlineHour="";
        if(onlineHour!=null)
        {
            if("-1".equalsIgnoreCase(onlineHour))
            {
                realOnlineHour="查无时长";
            }else if("0".equalsIgnoreCase(onlineHour))
            {
                realOnlineHour="手机状态异常";
            }else if("1".equalsIgnoreCase(onlineHour))
            {
                realOnlineHour="0至3个月";
            }else if("2".equalsIgnoreCase(onlineHour))
            {
                realOnlineHour="3至6个月";
            }else if("3".equalsIgnoreCase(onlineHour))
            {
                realOnlineHour="6至12个月";
            }else if("4".equalsIgnoreCase(onlineHour))
            {
                realOnlineHour="12至24个月";
            }else if("5".equalsIgnoreCase(onlineHour))
            {
                realOnlineHour="24个月以上";
            }
        }
        return realOnlineHour;
    }
    private String realCarrierCode(String carrierCode)
    {
        String realCarrierCode="";
        if(carrierCode!=null)
        {
            if("CMCC".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国移动";
            }else if("CUCC".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国联通";
            }else if("CTCC".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国电信";
            }else if("CBN".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国广电";
            }
        }
        return realCarrierCode;
    }
}
